package com.example.firstapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	 String risp="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	public void onResume(){
		super.onResume();
		
		final TextView tv = (TextView)findViewById(R.id.textView1); 
		final Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
            	//Perform action on click -> connessione al server
         		new Connection().execute();
         		//stampa in textbox
         		tv.setText(risp);
             }
         });
		
	}
	
	 private class Connection extends AsyncTask {
	        @Override
	        protected Object doInBackground(Object... arg0) {
	            connect();
	            return null;
	        }
	    }
	 
	    private void connect() {
	        try {
	            DefaultHttpClient client = new DefaultHttpClient();
	         //    HttpGet request = new HttpGet("http://10.0.2.2:8080/FirstWebApp/Controller");
	           HttpGet request = new HttpGet("http://10.0.2.2:8888/controller");

	            HttpResponse response = client.execute(request);
	            
	            //Log.i("ResponseMia",response.toString());
	            risp=inputStreamToString(response.getEntity().getContent());
	            Log.i("ResponseMia",risp);
	            
	        } catch (ClientProtocolException e) {
	            Log.d("HTTPCLIENT", e.getLocalizedMessage());
	        } catch (IOException e) {
	            Log.d("HTTPCLIENT", e.getLocalizedMessage());
	        }
	    }
	    
	    private String inputStreamToString(InputStream is) throws IOException {
	        String s = "";
	        String line = "";
	        // Wrap a BufferedReader around the InputStream
	        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
	        // Read response until the end
	        while ((line = rd.readLine()) != null) { s += line; }
	        // Return full string
	        return s;
	    }


}
